package com.elai.web.controller.baseconfig;

import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.core.domain.entity.SysUser;
import com.elai.common.core.page.TableDataInfo;
import com.elai.common.enums.BusinessType;
import com.elai.common.utils.SecurityUtils;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.baseconfig.domain.EventTypeDO;
import com.elai.baseconfig.service.EventTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 事件类型Controller
 *
 * @author elai
 * @date 2021-04-03
 */
@RestController
@RequestMapping("/config/type")
public class EventTypeController extends BaseController
{
    @Autowired
    private EventTypeService eventTypeService;

    /**
     * 查询事件类型列表
     */
    @PreAuthorize("@ss.hasPermi('config:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(EventTypeDO eventType)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            eventType.setLesseeId(currentUser.getLesseeId());
        }
        startPage();
        List<EventTypeDO> list = eventTypeService.selectEventTypeList(eventType);
        return getDataTable(list);
    }

    @GetMapping("/allEventTypeList")
    public AjaxResult allEventTypeList(EventTypeDO eventType)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            eventType.setLesseeId(currentUser.getLesseeId());
        }
        eventType.setStatus("Y");
        List<EventTypeDO> list = eventTypeService.selectEventTypeList(eventType);
        return AjaxResult.success(list);
    }

    /**
     * 导出事件类型列表
     */
    @PreAuthorize("@ss.hasPermi('config:type:export')")
    @Log(title = "事件类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EventTypeDO eventType)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            eventType.setLesseeId(currentUser.getLesseeId());
        }
        List<EventTypeDO> list = eventTypeService.selectEventTypeList(eventType);
        ExcelUtil<EventTypeDO> util = new ExcelUtil<EventTypeDO>(EventTypeDO.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取事件类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('config:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(eventTypeService.selectEventTypeById(id));
    }

    /**
     * 新增事件类型
     */
    @PreAuthorize("@ss.hasPermi('config:type:add')")
    @Log(title = "事件类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EventTypeDO eventType)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        eventType.setLesseeId(currentUser.getLesseeId());
        return toAjax(eventTypeService.insertEventType(eventType));
    }

    /**
     * 修改事件类型
     */
    @PreAuthorize("@ss.hasPermi('config:type:edit')")
    @Log(title = "事件类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EventTypeDO eventType)
    {
        return toAjax(eventTypeService.updateEventType(eventType));
    }

    /**
     * 删除事件类型
     */
    @PreAuthorize("@ss.hasPermi('config:type:remove')")
    @Log(title = "事件类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eventTypeService.deleteEventTypeByIds(ids));
    }
}
